package com.dongdongshop.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.dk.constant.CookieConstannt;
import com.dk.data.R;
import com.dongdongshop.pojo.User;
import com.dongdongshop.service.CartService;
import com.dongdongshop.util.CookUtil;
import com.dongdongshop.vo.CartVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//购物车
@Controller
@RequestMapping("cart")
public class CartController {

    @DubboReference
    private CartService cs;



    @RequestMapping("tiao")
    public String tiao(){
        return "cart";
    }


    //添加购物车未登录时候
    //跳转添加成功页面
    @RequestMapping("addCart")
    public String adCard (HttpServletRequest request, HttpServletResponse response, Long skuId, Integer num){
       addCartAjax(request,response,skuId,num);
        return "success-cart";
    }



    //没登录查询购物车数据
    @RequestMapping("getCartList")
    @ResponseBody
    public R<List<CartVO>>  getcartlist(HttpServletRequest request, HttpServletResponse response){
        //最后一步:如果没登录，我们购物车数据临时存放在cookie中
        //如果登录了，将cookie中数据取出来，合并到redis中,并且删除cookie中数据

        //先判断登录，如果没登录从cookie中取数据，登录成功从redis中取数据
        User user = (User) SecurityUtils.getSubject().getPrincipal();

        //我们在添加购物车之前，先获取原先的购物车数据，如果原先的购物车有数据就追加，没有的话就创建一个空
        String cookieValue = CookUtil.getCookieValue(request, CookieConstannt.CARTLIST,true);//取出cook
        if(StrUtil.isBlank(cookieValue)){//1.如果是空值
            cookieValue="[]";
        }
        List<CartVO> cart = JSONObject.parseArray(cookieValue, CartVO.class);//2.如果不是空值

        if(user==null){//从cookie取
            return R.ok().setData(cart);
        }else{//从redis中取
            List<CartVO> cartredis=  cs.getCartredis(user.getId());
             if(cart!=null&&cart.size()>0){
                 //合并，redis与cookie中数据合并
                 List<CartVO> margelist= cs.mergeCartlist(cart,cartredis);
                 //合并后存入redis,并删除cookie
                 cs.addCartredis(user.getId(),margelist);
                 CookUtil.deleteCookie(request,response,CookieConstannt.CARTLIST);
                 return R.ok().setData(margelist);
             }
            return R.ok().setData(cartredis);
        }
    }



    //加减数量
    @RequestMapping("addCartAjax")
    public R<List<CartVO>> addCartAjax (HttpServletRequest request, HttpServletResponse response, Long skuId, Integer num){
        addCarts(request,response,skuId,num);
        return getcartlist(request,response);
    }



    //封装方法
    private void addCarts(HttpServletRequest request, HttpServletResponse response, Long skuId, Integer num){
        //我们在添加购物车之前，先获取原先的购物车数据，如果原先的购物车有数据就追加，没有的话就创建一个空
       R<List<CartVO>> r = getcartlist(request,response);
        List<CartVO> cartlist = r.getData();
        //3.调用购物车服务，帮我们追加购物车数据
        cartlist=cs.addCard(cartlist, skuId, num);
        //如果没登录，将购物车保存到cookie中，登录成功，保存到redis中
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            //保存到cook中
            CookUtil.setCookie(request,response,CookieConstannt.CARTLIST,JSONObject.toJSONString(cartlist),true);
        }else{
            //保存到redis
            cs.addCartredis(user.getId(), cartlist);
        }
    }



}
